package org.example.interncandidate.service.file.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.example.interncandidate.exception.AssertionException;
import org.example.interncandidate.mapper.file.FileMapper;
import org.example.interncandidate.model.file.po.File;
import org.example.interncandidate.model.file.vo.FileVO;
import org.example.interncandidate.service.file.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements FileService {
//    @Value("${minio.endpoint}")
//    private  String MINIO_ENDPOINT;
//    @Value("${minio.username}")
//    private  String MINIO_ACCESS_KEY;
//    @Value("${minio.password}")
//    private  String MINIO_SECRET_KEY;
//    @Value("${minio.bucketName}")
//    private  String BUCKET_NAME;
    private static final String MINIO_ENDPOINT = "http://47.96.164.80:9000";
    private static final String MINIO_ACCESS_KEY = "root";
    private static final String MINIO_SECRET_KEY = "ZBzwh8510#";
    private static final String BUCKET_NAME = "public";
    @Autowired
    private FileMapper fileMapper;
    @Override
    public FileVO uploadFile(MultipartFile file) {
        try {
            // 1. 创建 MinioClient 实例
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(MINIO_ENDPOINT)
                    .credentials(MINIO_ACCESS_KEY, MINIO_SECRET_KEY)
                    .build();

            // 2. 检查存储桶是否存在，不存在则创建
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder()
                    .bucket(BUCKET_NAME)
                    .build());
            if (!found) {
                minioClient.makeBucket(MakeBucketArgs.builder()
                        .bucket(BUCKET_NAME)
                        .build());
                System.out.println("Created bucket " + BUCKET_NAME);
            }
            // 3. 上传文件
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(BUCKET_NAME)
                            .object(file.getOriginalFilename())
                            .stream(file.getInputStream(), file.getSize(), -1)
                            .build());

            System.out.println("File uploaded successfully: " + file.getOriginalFilename());
            // 4. 获取永久 URL（需要设置存储桶策略为公开或使用永久访问凭证）
            // 方法1：如果存储桶是公开的
            String publicUrl = MINIO_ENDPOINT + "/" + BUCKET_NAME + "/" + file.getOriginalFilename();
            System.out.println("Public URL: " + publicUrl);
        }catch (Exception e){
            throw new AssertionException(500,"上传失败");
        }
        return null;
    }
}
